package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatTextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.EditBloodInfoBean
import com.gexiaobao.pigeon.databinding.ItemFourV4StyleBinding

/**
 * @Author      : hxw
 * @Date        : 2024/9/21 9:51
 * @Describe    :
 */
class AdapterFourV4Style(data: ArrayList<EditBloodInfoBean>) :
    BaseQuickAdapter<EditBloodInfoBean, DataBindBaseViewHolder>(R.layout.item_four_v4_style, data) {

    private var rHeight: Int = 0

    override fun convert(holder: DataBindBaseViewHolder, item: EditBloodInfoBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemFourV4StyleBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        val title = when (holder.layoutPosition) {
            0 -> context.getString(R.string.great_grandfather_grandmother)
            1 -> context.getString(R.string.great_grandmother_w)//"曾外祖父/母"
            2 -> context.getString(R.string.maternal_great_grandfather)//"外曾祖父/母"
            else -> context.getString(R.string.My_great_grandfather)//"外曾外祖父/母"
        }
        holder.setText(R.id.tvItemTitleFourV, title)

        if (holder.layoutPosition > 3) {
            holder.setGone(R.id.tvItemTitleFourV, true)
        } else {
            holder.setGone(R.id.tvItemTitleFourV, false)
        }

        val sexIcon: Int
        val bg: Int
        if (holder.layoutPosition <= 3) {
            sexIcon = R.mipmap.icon_male
            bg = R.drawable.round8_male_pigeon_linear_bg
        } else {
            sexIcon = R.mipmap.icon_female
            bg = R.drawable.round8_female_pigeon_linear_bg

        }
        holder.setImageResource(R.id.ivItemSex, sexIcon)
        holder.setBackgroundResource(R.id.tvItemRingId, bg)

        val params: ViewGroup.LayoutParams = holder.itemView.layoutParams
        if (rHeight == 0) {
            params.height = 180
        } else {
            if (holder.layoutPosition == 4 || holder.layoutPosition == 5 || holder.layoutPosition == 6 || holder.layoutPosition == 7) {
                params.height = rHeight / 2 - 16
            } else {
                params.height = rHeight / 2 + 16
            }
        }
        holder.itemView.layoutParams = params

        // 动态设置边距
        val layoutParams = holder.itemView.layoutParams as ViewGroup.MarginLayoutParams
        if (holder.layoutPosition == 4 || holder.layoutPosition == 5 || holder.layoutPosition == 6 || holder.layoutPosition == 7) {
            layoutParams.setMargins(0, 0, 0, 10)
        } else {
            layoutParams.setMargins(0, 0, 0, 0)
        }
        holder.itemView.layoutParams = layoutParams
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setLayoutHeight(height: Int) {
        this.rHeight = height
        notifyDataSetChanged()
    }

    // 更新单个 item 的方法
    fun updateItem(position: Int, dataBean: EditBloodInfoBean?) {
        if (position >= 0 && position < data.size) {
            val item: EditBloodInfoBean = data[position]
            if (dataBean != null) {
                item.country = dataBean.country
                item.countryEn = dataBean.countryEn
                item.featherColor = dataBean.featherColor
                item.ringId = dataBean.ringId
                item.name = dataBean.name
                item.sex = dataBean.sex
                item.eye = dataBean.eye
                item.gradesDesc = dataBean.gradesDesc
                item.title = dataBean.title
                item.level = dataBean.level
                item.levelInfo = dataBean.levelInfo
            }
            notifyItemChanged(position)
        }
    }
}